/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using Ngilead.Core.Replicator;
using log4net;
using Ngilead.Core;
using Ngilead.Core.Store;
using Ngilead.Core.Utils;
using System.Collections.Generic;
using System.Reflection;

namespace Ngilead.Core.Replicator.Clone
{
/**
 * Bean replicator with different from and to classes for clone operation
 * @author bruno.marchesson
 *
 */
    public class CloneInstanceReplicator : InstanceReplicator
    {
        /*----------------------------------------------------------------------------------------*/
        #region Attributes

        /// <summary> Logger channel</summary>
        private static ILog logger = LogManager.GetLogger(typeof(CloneInstanceReplicator));

        /// <summary>Persistence util class</summary>
        private IPersistenceUtil _persistenceUtil;

        #endregion
        /*----------------------------------------------------------------------------------------*/

        /*----------------------------------------------------------------------------------------*/
        #region Properties

        /// <summary>
        /// The associated persistence util
        /// </summary>
        public IPersistenceUtil PersistenceUtil
        {
            get { return _persistenceUtil; }
            set { _persistenceUtil = value; }
        }

        #endregion
        /*----------------------------------------------------------------------------------------*/


        /*----------------------------------------------------------------------------------------*/
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public CloneInstanceReplicator()
        {
        }

        #endregion
        /*----------------------------------------------------------------------------------------*/

        /*----------------------------------------------------------------------------------------*/
        #region Overriden
        /// <summary>
        /// Replicate bean
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="from"></param>
        /// <returns></returns>
        public override T ReplicateInstance<T>(T from)
        {
            // Force persistence map computation (useful for component type subclasses)
            PersistenceUtil.IsPersistentPojo(from);

            return base.ReplicateInstance(from);
        }

        /// <summary>
        /// Create a new instance of the instance to clone.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="from"></param>
        /// <returns></returns>
        protected override T CreateNewInstance<T>(T from)
        {
            Type typeClass = from.GetType();
            typeClass = PersistenceUtil.GetUnenhancedClass(typeClass);
            return (T)Activator.CreateInstance(typeClass);
        }

        #endregion
        /*----------------------------------------------------------------------------------------*/
    }
}